<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/10/04 23:22:59 $)
// $RCSfile: functions_xmlapi.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Renders the XML Tree
*/
function renderXMLTree(&$xml, $departmentidlist, $ismobile = false)
{
	global $_SWIFT;

		foreach ($_SWIFT["departmentcache"] as $key=>$val)
		{
			if ($val["departmentmodule"] != MODULE_TICKETS || !in_array($val["departmentid"], $departmentidlist))
			{
				continue;
			}

			$ticketcount = "(".$_SWIFT["ticketcountcache"][$val["departmentid"]]["totaltickets"].")";
			if ($ticketcount == "()")
			{
				$ticketcount = "(0)";
			}
			unset($ticketcount);

			$_departmenttitle = $val["title"];

			if ($_SWIFT["ticketcountcache"][$val["departmentid"]]["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
			{
				$_departmentisnew = true;
			} else {
				$_departmentisnew = false;
			}

			$xml->addParentTag("department");
				$xml->addTag("title", $_departmenttitle);
				$xml->addTag("isnew", iif($_departmentisnew==true,"1","0"));
				if ($ismobile)
				{
					$xml->addTag("departmentid", $val["departmentid"]);
				} else {
					$xml->addTag("link", $_SWIFT["swiftpath"]."staff/index.php?_m=tickets&_a=manage&departmentid=".intval($val["departmentid"]));
				}

				foreach ($_SWIFT["statuscache"] as $statuskey=>$statusval)
				{
					if ($statusval["departmentid"] != 0 && $statusval["departmentid"] != $val["departmentid"])
					{
						continue;
					}

					if ($_SWIFT["ticketcountcache"][$val["departmentid"]][$statusval["ticketstatusid"]]["lastactivity"] > $_SWIFT["staff"]["lastvisit"])
					{
						$_statusisnew = true;
					} else {
						$_statusisnew = false;
					}

					$statuscount = intval($_SWIFT["ticketcountcache"][$val["departmentid"]][$statusval["ticketstatusid"]]["count"]);
					if ($statuscount == "0" || $statusval["displaycount"] != 1)
					{
						unset($statuscount);
					}

					$_statustitle = $statusval["title"];

					$xml->addParentTag("status");
						$xml->addTag("title", $_statustitle);
						$xml->addTag("isnew", iif($_statusisnew==true, "1", "0"));
						$xml->addTag("count", intval($statuscount));
						if ($ismobile)
						{
							$xml->addTag("ticketstatusid", $statusval["ticketstatusid"]);
						} else {
							$xml->addTag("link", $_SWIFT["swiftpath"]."staff/index.php?_m=tickets&_a=manage&departmentid=".$val["departmentid"]."&ticketstatusid=".$statusval["ticketstatusid"]);
						}
					$xml->endParentTag("status");
				}

				$xml->endParentTag("department");
		}
}

?>